package android.slc.appbase.ui.vd;

import android.slc.appbase.entity.ErrorMsg;
import android.slc.appbase.ui.vd.wrapper.IBaseListWrapper;
import android.slc.box.core.property.WrapDataChangeListener;

import java.io.Serializable;
import java.util.List;

/**
 * 基础的单次加载盒子vm
 *
 * @author slc
 * @date 2020-08-25 16:13
 */
public abstract class BaseListVd<T extends Serializable, VB extends BaseListVmBox<T>> extends BaseListCommonVd<T, VB> {

    private final WrapDataChangeListener<List<T>> dataListChangeListener = new WrapDataChangeListener<List<T>>() {

        @Override
        public void onChanged(List<T> value) {
            notifyRecyclerRefresh(vmBox.getDataList());
        }
    };

    private final WrapDataChangeListener<ErrorMsg> errorMsgChangeListener = new WrapDataChangeListener<ErrorMsg>() {

        @Override
        public void onChanged(ErrorMsg value) {
            notifyRefreshError(value);
        }
    };
    private final WrapDataChangeListener<Void> startRefreshListener = new WrapDataChangeListener<Void>() {

        @Override
        public void onChanged(Void value) {
            notifyRefreshing();
        }
    };

    protected IBaseListWrapper<?> getListPageWrapper() {
        if (vmBox != null) {
            return vmBox;
        }
        throw new RuntimeException("vmBox没有实现IBaseListWrapper接口");
    }

    @Override
    protected void initMustCondition() {
        super.initMustCondition();
        this.vmBox.getDataListObserver().addListener(dataListChangeListener);
        this.vmBox.getErrorMsgObservable().addListener(errorMsgChangeListener);
        getListPageWrapper().getStartRefreshOf().addListener(startRefreshListener);
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.vmBox.getDataListObserver().removeListener(dataListChangeListener);
        this.vmBox.getErrorMsgObservable().removeListener(errorMsgChangeListener);
        this.getListPageWrapper().getStartRefreshOf().removeListener(startRefreshListener);
    }

    protected void notifyRecyclerRefresh(List<T> list) {

    }

    protected void notifyRefreshError(ErrorMsg errorMsg) {

    }
}
